package xyz.jcat.seata.example.storage.dubbo;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.jcat.common.exception.BusinessException;
import xyz.jcat.seata.example.api.IStorageService;
import xyz.jcat.seata.example.api.dto.StorageDecrDto;
import xyz.jcat.seata.example.api.exception.ProviderException;
import xyz.jcat.seata.example.storage.domain.dao.IItemStorageDao;

import java.util.Date;

@DubboService
public class StorageService implements IStorageService {

    @Autowired
    private IItemStorageDao itemStorageDao;

    @Override
    public void decr(StorageDecrDto storageDecrDto) {
        int result = itemStorageDao.decr(storageDecrDto.getItemId(), storageDecrDto.getNum(), new Date());
        if(!SqlHelper.retBool(result)) {
            //throw new ProviderException("商品库存不足");
            throw new BusinessException("商品库存不足");
        }
    }
}
